package com.yangfei.SportAssistant.mapProvider;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.yangfei.SportAssistant.MapHelper;
import com.yangfei.SportAssistant.R;
import com.yangfei.SportAssistant.util.Constants;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BaiduMap extends MapActivity implements LoggerMap
{
	@Override
	// from MapActivity
	protected boolean isRouteDisplayed()
	{
		// TODO Auto-generated method stub
		return false;
	}

	private MapView mMapView;
	private BMapManager mBMapMan = null;

	private TextView[] mSpeedtexts;
	private TextView mLastGPSSpeedView;
	private TextView mLastGPSAltitudeView;
	private TextView mDistanceView;
	private MyLocationOverlay mMylocation;
	private SharedPreferences mSharedPreferences;
	
	private MapHelper mMapHelper = null;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	protected void onCreate(Bundle load)
	{
		super.onCreate(load);
		mMapHelper = new MapHelper(this,load);
		
		setContentView(R.layout.map_baidu);

		mMapView = (MapView) findViewById(R.id.myMapView);
		mBMapMan = new BMapManager(this);
		mBMapMan.init(Constants.mStrKey, null);
		mBMapMan.getLocationManager().setNotifyInternal(2, 10);
		//mBMapMan.getLocationManager().enableProvider(MKLocationManager.MK_GPS_PROVIDER);
		super.initMapActivity(mBMapMan);

		mMylocation = new MyLocationOverlay(this, mMapView);
		mMapView.setBuiltInZoomControls(true);
		mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		mMapHelper.Init();
	}

	@Override
	protected void onResume()
	{
		if(mMapHelper != null)
			mMapHelper.onResume();
		
		if (mBMapMan != null)
			mBMapMan.start();

		super.onResume();
	}

	@Override
	protected void onPause()
	{
		if(mMapHelper != null)
			mMapHelper.onPause();
		
		if (mBMapMan != null)
			mBMapMan.stop();

		super.onPause();
	}

	@Override
	protected void onDestroy()
	{
		if(mMapHelper != null)
			mMapHelper.onDestroy();
		super.onDestroy();
	}

	@Override
	public void onNewIntent(Intent newIntent)
	{}

	@Override
	protected void onRestoreInstanceState(Bundle load)
	{
		if (load != null)
		{
			super.onRestoreInstanceState(load);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle save)
	{
		super.onSaveInstanceState(save);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		boolean result = super.onCreateOptionsMenu(menu);
		return result;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu)
	{
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		boolean handled = false;
		if (!handled)
		{
			handled = super.onOptionsItemSelected(item);
		}
		return handled;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		super.onActivityResult(requestCode, resultCode, intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		boolean propagate = true;
		switch (keyCode)
		{
			default:
				if (propagate)
				{
					propagate = super.onKeyDown(keyCode, event);
				}
				break;
		}
		return propagate;
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		Dialog dialog = null;
		if (dialog == null)
		{
			dialog = super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog)
	{
		super.onPrepareDialog(id, dialog);
	}

	/******************************/
	/** Own methods **/
	/******************************/

	private void setTrafficOverlay(boolean b)
	{
		SharedPreferences sharedPreferences = mSharedPreferences;
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(Constants.TRAFFIC, b);
		editor.commit();
	}

	private void setSatelliteOverlay(boolean b)
	{
		SharedPreferences sharedPreferences = mSharedPreferences;
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(Constants.SATELLITE, b);
		editor.commit();
	}

	/******************************/
	/** Loggermap methods **/
	/******************************/

	public void updateOverlays()
	{
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
		mMapView.setSatellite(sharedPreferences.getBoolean(Constants.SATELLITE, false));
		mMapView.setTraffic(sharedPreferences.getBoolean(Constants.TRAFFIC, false));
	}

	public void setDrawingCacheEnabled(boolean b)
	{
		findViewById(R.id.mapScreen).setDrawingCacheEnabled(true);
	}

	public Activity getActivity()
	{
		return this;
	}

	public void onLayerCheckedChanged(int checkedId, boolean isChecked)
	{
		/*
		 * switch (checkedId) { case R.id.layer_google_satellite:
		 * setSatelliteOverlay(true); break; case R.id.layer_google_regular:
		 * setSatelliteOverlay(false); break; case R.id.layer_traffic:
		 * setTrafficOverlay(isChecked); break; default: break; }
		 */
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
	{
		if (key.equals(Constants.TRAFFIC))
		{
			updateOverlays();
		}
		else if (key.equals(Constants.SATELLITE))
		{
			updateOverlays();
		}
	}

	public Bitmap getDrawingCache()
	{
		return findViewById(R.id.mapScreen).getDrawingCache();
	}

	public void showMediaDialog(BaseAdapter mediaAdapter)
	{}

	public void onDateOverlayChanged()
	{
		mMapView.postInvalidate();
	}

	public String getDataSourceId()
	{
		return Constants.OSM_PROVIDER;
	}

	public boolean isOutsideScreen(GeoPointYf lastPoint)
	{
		Point out = new Point();
		this.mMapView.getProjection().toPixels(convertGeoPoint(lastPoint), out);
		int height = this.mMapView.getHeight();
		int width = this.mMapView.getWidth();
		return (out.x < 0 || out.y < 0 || out.y > height || out.x > width);
	}

	public boolean isNearScreenEdge(GeoPointYf lastPoint)
	{
		Point out = new Point();
		this.mMapView.getProjection().toPixels(convertGeoPoint(lastPoint), out);
		int height = this.mMapView.getHeight();
		int width = this.mMapView.getWidth();
		return (out.x < width / 4 || out.y < height / 4 || out.x > (width / 4) * 3 || out.y > (height / 4) * 3);
	}

	public void executePostponedActions()
	{
		// NOOP for Google Maps
	}

	public void enableCompass()
	{
		mMylocation.enableCompass();
	}

	public void enableMyLocation()
	{
		mMylocation.enableMyLocation();
	}

	public void disableMyLocation()
	{
		mMylocation.disableMyLocation();
	}

	public void disableCompass()
	{
		mMylocation.disableCompass();
	}

	public void setZoom(int zoom)
	{
		mMapView.getController().setZoom(zoom);
	}

	public void animateTo(GeoPointYf storedPoint)
	{
		mMapView.getController().animateTo(convertGeoPoint(storedPoint));
	}

	public int getZoomLevel()
	{
		return mMapView.getZoomLevel();
	}

	public GeoPointYf getMapCenter()
	{
		return convertOSMGeoPoint(mMapView.getMapCenter());
	}

	public boolean zoomOut()
	{
		return mMapView.getController().zoomOut();
	}

	public boolean zoomIn()
	{
		return mMapView.getController().zoomIn();
	}

	public void postInvalidate()
	{
		mMapView.postInvalidate();
	}

	public void addOverlay(OverlayProvider overlay)
	{
		mMapView.getOverlays().add(overlay.getBaiduOverlay());
	}

	public void clearAnimation()
	{
		mMapView.clearAnimation();
	}

	public void setCenter(GeoPointYf lastPoint)
	{
		mMapView.getController().setCenter(convertGeoPoint(lastPoint));
	}

	public int getMaxZoomLevel()
	{
		return mMapView.getMaxZoomLevel();
	}

	public GeoPointYf fromPixels(int x, int y)
	{
		GeoPoint osmGeopoint = mMapView.getProjection().fromPixels(x, y);
		GeoPointYf geopoint = convertOSMGeoPoint(osmGeopoint);
		return geopoint;
	}

	public void toPixels(GeoPointYf geoPoint, Point screenPoint)
	{
		mMapView.getProjection().toPixels(convertGeoPoint(geoPoint), screenPoint);
	}

	public boolean hasProjection()
	{
		return mMapView.getProjection() != null;
	}

	public float metersToEquatorPixels(float float1)
	{
		return mMapView.getProjection().metersToEquatorPixels(float1);
	}

	public TextView[] getSpeedTextViews()
	{
		return mSpeedtexts;
	}

	public TextView getAltitideTextView()
	{
		return mLastGPSAltitudeView;
	}

	public TextView getSpeedTextView()
	{
		return mLastGPSSpeedView;
	}

	public TextView getDistanceTextView()
	{
		return mDistanceView;
	}

	static GeoPoint convertGeoPoint(GeoPointYf point)
	{
		GeoPoint geopoint = new GeoPoint(point.getLatitudeE6(), point.getLongitudeE6());
		return geopoint;
	}

	static GeoPointYf convertOSMGeoPoint(GeoPoint point)
	{
		return new GeoPointYf(point.getLatitudeE6(), point.getLongitudeE6());
	}

	public void clearOverlays()
	{
		mMapView.getOverlays().clear();
	}

	public SlidingIndicatorView getScaleIndicatorView()
	{
		return null;
	}

	public ViewGroup getMapView()
	{
		return mMapView;
	}

	public boolean isSatelliteView()
	{
		return mMapView.isSatellite();
	}

	public void setSatelliteView(boolean mapMode)
	{
		mMapView.setSatellite(mapMode);
	}

	public int getLatitudeSpan()
	{
		return mMapView.getLatitudeSpan();
	}

	public int getLongitudeSpan()
	{
		return mMapView.getLongitudeSpan();
	}

	public void zoomToSpan(int latSpan, int longSpan)
	{
		mMapView.getController().zoomToSpan(latSpan, longSpan);
	}
	
	public void showMyLocation()
	{
		mMapHelper.showMyLocation();
	}
	
	public void showWaypoint(long trackId, long id)
	{
		mMapHelper.showWaypoint(trackId,id);
	}
	public void showWaypoint(long id)
	{
		mMapHelper.showWaypoint(id);
	}
	
	public void draw(Canvas canvas)
	{
		mMapHelper.draw(canvas);
	}
}
